#ifndef STORAGE_SYSTEM_H
#define STORAGE_SYSTEM_H

#include <stddef.h>
#include <stdint.h>

// 存储系统错误码
typedef enum {
    STORAGE_OK = 0,
    STORAGE_ERROR_IO = 1,
    STORAGE_ERROR_MEMORY = 2,
    STORAGE_ERROR_NOT_FOUND = 3,
    STORAGE_ERROR_INVALID = 4,
    STORAGE_ERROR_FULL = 5,
    STORAGE_ERROR_BUSY = 6,
    STORAGE_ERROR_CATALOG = 7  // 添加目录错误码
} storage_error_t;

#define PAGE_SIZE 4096 // 4KB页面大小
#define MAX_PAGES 1024 // 最大页面数
#define CACHE_SIZE 16  // 缓存大小

typedef uint32_t page_id_t;

// 页面数据类型
typedef uint8_t page_t[PAGE_SIZE];

// 缓存替换策略枚举
typedef enum {
    CACHE_LRU,
    CACHE_FIFO
} cache_replacement_policy_t;

// 系统初始化与关闭
extern storage_error_t init_storage_system(const char* data_dir);
extern storage_error_t shutdown_storage_system();
extern storage_error_t shutdown_storage_system_ensure_persistence();  // 添加这行
// 添加函数声明
int init_storage_system_with_catalog(const char *data_dir, void* catalog);

// 页面管理操作
page_id_t allocate_page();
storage_error_t free_page(page_id_t page_id);

// 页面读写操作
storage_error_t read_page(page_id_t page_id, uint8_t* buffer);
storage_error_t write_page(page_id_t page_id, const uint8_t* data);
storage_error_t get_page(page_id_t page_id, uint8_t* buffer);

// 缓存刷新操作
storage_error_t flush_page(page_id_t page_id);
storage_error_t flush_all_pages();

// 统计信息
uint32_t get_cache_hits();
uint32_t get_cache_misses();
void print_cache_stats();
void print_replacement_log();

#endif // STORAGE_SYSTEM_H